package com.bwie.config;

import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.IOException;


/**
 * @Auther:${涛}
 * @Date:2024/9/2 11:53
 * @Description: KafkaConfig
 * @Version 1.0.0
 */
@Configuration
public class KafkaConfig {


    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @RabbitListener(queues = "0902")
    public void setRabbitTemplate(String msg, Message message, Channel channel) throws IOException {
        System.out.println("进入url:" + msg);
        try {
//            String msgBody = stringRedisTemplate.opsForValue().get(msg);
            if (StringUtils.isEmpty(msg)) {
                System.out.println("已经被消费过不能重复消费" + msg);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            } else {
                System.out.println("消费成功--------");
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
                stringRedisTemplate.delete(msg);
            }

        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            throw new RuntimeException(e);
        }

    }
}


